const request = require('supertest');
const express = require('express');
const fs = require('fs-extra');
const path = require('path');
const DeploymentService = require('../server/services/deployment');

// 模拟Express应用用于测试2122112
const app = express();
app.use(express.json());

// 测试配置
const TEST_DIR = path.join(__dirname, 'test-data');
const TEST_DEPLOY_DIR = path.join(TEST_DIR, 'deployments');
const TEST_UPLOAD_DIR = path.join(TEST_DIR, 'uploads');

describe('Deployment Service', () => {
  let deploymentService;

  beforeAll(async () => {
    // 创建测试目录
    await fs.ensureDir(TEST_DEPLOY_DIR);
    await fs.ensureDir(TEST_UPLOAD_DIR);
    deploymentService = new DeploymentService(TEST_DEPLOY_DIR);
  });

  afterAll(async () => {
    // 清理测试目录
    await fs.remove(TEST_DIR);
  });

  beforeEach(async () => {
    // 每次测试前清理部署目录
    await fs.emptyDir(TEST_DEPLOY_DIR);
  });

  describe('项目部署功能', () => {
    test('应该能够创建项目目录结构', async () => {
      const projectId = 'test-project-1';
      const projectDir = path.join(TEST_DEPLOY_DIR, projectId);
      
      await fs.ensureDir(projectDir);
      expect(await fs.pathExists(projectDir)).toBe(true);
    });

    test('应该能够检测项目类型', async () => {
      const testDir = path.join(TEST_DEPLOY_DIR, 'test-detection');
      await fs.ensureDir(testDir);
      
      // 创建package.json
      const packageJson = {
        name: 'test-project',
        version: '1.0.0',
        scripts: { start: 'node index.js' },
        dependencies: { express: '^4.18.0' }
      };
      await fs.writeJson(path.join(testDir, 'package.json'), packageJson);
      
      const config = await deploymentService.detectProjectType(testDir);
      expect(config.type).toBe('node');
      expect(config.framework).toBe('express');
    });

    test('应该能够检测静态项目', async () => {
      const testDir = path.join(TEST_DEPLOY_DIR, 'test-static');
      await fs.ensureDir(testDir);
      
      await fs.writeFile(path.join(testDir, 'index.html'), '<html><body>Test</body></html>');
      
      const config = await deploymentService.detectProjectType(testDir);
      expect(config.type).toBe('static');
    });
  });

  describe('版本管理', () => {
    test('应该能够清理旧版本', async () => {
      const projectId = 'test-cleanup';
      const projectDir = path.join(TEST_DEPLOY_DIR, projectId);
      
      // 创建多个版本
      for (let i = 0; i < 7; i++) {
        const versionDir = path.join(projectDir, `version-${i}`);
        await fs.ensureDir(versionDir);
        await fs.writeFile(path.join(versionDir, 'test.txt'), `Version ${i}`);
      }
      
      await deploymentService.cleanupOldVersions(projectId);
      
      const versions = await fs.readdir(projectDir);
      expect(versions.length).toBeLessThanOrEqual(5);
    });
  });

  describe('端口管理', () => {
    test('应该能够为项目分配端口', () => {
      const projectId1 = 'project-1';
      const projectId2 = 'project-2';
      
      const port1 = deploymentService.findAvailablePort(projectId1);
      const port2 = deploymentService.findAvailablePort(projectId2);
      
      expect(port1).toBeGreaterThanOrEqual(3000);
      expect(port2).toBeGreaterThanOrEqual(3000);
      expect(port1).not.toBe(port2);
    });
  });

  describe('日志系统', () => {
    test('应该能够记录部署日志', () => {
      const projectId = 'test-logs';
      const message = 'Test log message';
      
      deploymentService.logDeployment(projectId, message);
      
      const logs = deploymentService.getLogs(projectId);
      expect(logs).toHaveLength(1);
      expect(logs[0].message).toBe(message);
      expect(logs[0].level).toBe('info');
    });

    test('应该能够限制日志数量', async () => {
      const projectId = 'test-log-limit';
      
      // 添加大量日志
      for (let i = 0; i < 1500; i++) {
        deploymentService.logDeployment(projectId, `Log ${i}`);
      }
      
      const logs = deploymentService.getLogs(projectId);
      expect(logs.length).toBeLessThanOrEqual(1000);
    });
  });
});